package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import model.Database;
import model.SearchContact;
import controller.ActionAdmin;
import controller.Engine;
import controller.GhostTextField;
import controller.SearchKeyListener;

@SuppressWarnings("serial")
public class AdminContactView extends JFrame implements Observer{

	static JFrame contactView = new JFrame();
	static JPanel contact = new JPanel();
	static JPanel contactContainer = new JPanel();
	static JScrollPane scroll = null;
	static ActionListener action = new ActionAdmin();
	static int contactCount = 2;
	static Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
	static KeyListener key = new SearchKeyListener();
	JPanel scrollContainer;
	SearchContact c = new SearchContact();
	JPanel searchPanel;
	JLabel img = Engine.setImage("search.jpg", 47, 47);
	public static JTextField search = new JTextField("",23);
	public static String text="";
	static boolean flag;
	public static Vector<Integer> selectContact = new Vector<Integer>();
	static Observer obs;

	//@SuppressWarnings("unused")
	public AdminContactView(boolean flag) {
		AdminContactView.flag=flag;
		contactView  = this;
		obs = this;
		if (flag)
			contactView.addWindowListener(new WindowAdapter()
			{
				@Override
				public void windowClosing(WindowEvent e)
				{
					SearchContact.cntAdm.deleteObservers();
					search.removeKeyListener(key);
					Engine.restartHome("admin");
				}
			});
		else{
			contactView.addWindowListener(new WindowAdapter()
			{
				@Override
				public void windowClosing(WindowEvent e)
				{
					selectContact.clear();
					SearchContact.cntAdm.deleteObserver(obs);
					search.removeKeyListener(key);
					HomeContact.restartSearch();
					Engine.restartMenu();
					Engine.restartHome("admin");
				}
			});
			HomeContact.pauseSearch();
		}
		startAdminContactView();
	}

	@SuppressWarnings("unused")
	public void startAdminContactView(){
		if (SearchContact.cntAdm.countObservers()!=0)
			SearchContact.cntAdm.deleteObservers();
		SearchContact.cntAdm.addObserver(this);
		if (search.getKeyListeners().length!=0)
			search.removeKeyListener(key);
		search.addKeyListener(key);
		search.setName("Admin");
		GhostTextField focus = new GhostTextField(search,"CERCA");
		search.setName("admin");
		contact.setBackground(Color.white);
		searchPanel = new JPanel();
		searchPanel.setLayout(new BoxLayout(searchPanel,BoxLayout.X_AXIS));
		searchPanel.setMinimumSize(new Dimension(340,38));
		searchPanel.setPreferredSize(new Dimension(340,38));
		searchPanel.setMaximumSize(new Dimension(340,38));
		searchPanel.setAlignmentX(CENTER_ALIGNMENT);
		searchPanel.setAlignmentY(CENTER_ALIGNMENT);
		searchPanel.setBackground(Color.white);
		searchPanel.add(Box.createHorizontalStrut(8));
		searchPanel.add(img);
		searchPanel.add(Box.createHorizontalStrut(2));
		searchPanel.add(search);

		contactContainer.setLayout(new BoxLayout(contactContainer,BoxLayout.Y_AXIS));
		contactContainer.setBackground(Color.white);
		scroll= new JScrollPane(contactContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setMinimumSize(new Dimension(340,340));
		scroll.setPreferredSize(new Dimension(340,340));
		scroll.setMaximumSize(new Dimension(340,340));
		scroll.setVisible(true);

		setSearchedContactList(text);

		scrollContainer = new JPanel();
		scrollContainer.add(Box.createHorizontalStrut(0));
		scrollContainer.add(scroll);
		scrollContainer.setBackground(Color.white);
		contact.setLayout(new BoxLayout(contact, BoxLayout.Y_AXIS));
		contact.add(Box.createVerticalStrut(12));

		contact.add(searchPanel);
		contact.add(Box.createVerticalStrut(6));
		contact.add(scrollContainer);
		contact.setMinimumSize(new Dimension(350,448));
		contact.setPreferredSize(new Dimension(350,448));
		contact.setMaximumSize(new Dimension(350,448));
		contact.setVisible(true);
		contact.validate();
		contactView.setIconImage(Engine.immaginelog);
		contactView.setTitle("Admin: Allert-Contatti");
		contactView.add(contact);
		contactView.setVisible(true);
		contactView.pack();
	}

	public static void setSearchedContactList(String txt){
		SearchContact.cnt.Search(txt);
		contactContainer.removeAll();
		if (txt.isEmpty() || search.getForeground()==Color.lightGray){
			contactCount=0;
			contactContainer.removeAll();
			JButton descr = new JButton();
			descr.setPreferredSize(new Dimension(318,24));
			descr.add(new JLabel("Utenti: "));
			descr.setFocusable(false);
			descr.setContentAreaFilled(false);
			contactContainer.add(descr);
			if (Database.allAcc.size()!=0)
				for (int i=0;i<Database.allAcc.size();i++){
					JPanel contactPanel = new JPanel();
					contactPanel.setBackground(new Color(0,0,0,0));
					JButton contatto = new JButton();
					contatto.setMaximumSize(new Dimension(340,36));
					for (int j=0;j<selectContact.size();j++)
						if (selectContact.get(j)==Database.Utente.getRubrica().get(i).getID()){
							contatto.setBackground(Color.green);
							break;
						}
					Component comp = new Component(){
					};
					comp.setName(""+Database.allAcc.get(i).getID());
					contactPanel.setLayout(new BoxLayout(contactPanel,BoxLayout.X_AXIS));
					contactPanel.add(Engine.setImage(Database.allAcc.get(i).getPhoto(),50,50));
					contactPanel.add(Box.createHorizontalStrut(5));
					contactPanel.add(new JLabel(Database.allAcc.get(i).getFirstname()+" "+Database.allAcc.get(i).getSecondname()));
					contactPanel.add(Box.createHorizontalGlue());
					contatto.add(comp);
					contatto.add(contactPanel);
					contatto.addActionListener(action);
					//contatto.setAlignmentX(LEFT_ALIGNMENT);
					if (flag)
						contatto.setName("alertContact");
					else
						contatto.setName("viewEventContact");
					contactContainer.add(contatto);
				}
			contactCount = contactContainer.getComponentCount();
			if (contactCount==1){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("Non sono presenti utenti in LoAD!"));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				contactCount = contactContainer.getComponentCount();
			}
		}
		else
		{
			if (!SearchContact.search.isEmpty()){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("'"+txt+"' trovati in LoAD: "));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
			}
			for (int i=0;i<SearchContact.search.size();i++){
				JPanel contactPanel = new JPanel();
				contactPanel.setBackground(new Color(0,0,0,0));
				JButton contatto = new JButton();
				contatto.setMaximumSize(new Dimension(340,36));
				Component comp = new Component(){
				};
				comp.setName(""+SearchContact.search.get(i).getID());
				contatto.setName("selectContact");
				contactPanel.setLayout(new BoxLayout(contactPanel,BoxLayout.X_AXIS));
				contactPanel.add(Engine.setImage(SearchContact.search.get(i).getPhoto(),50,50));
				contactPanel.add(Box.createHorizontalStrut(5));
				contactPanel.add(new JLabel(SearchContact.search.get(i).getFirstname()+" "+SearchContact.search.get(i).getSecondname()));
				contactPanel.add(Box.createHorizontalGlue());
				contatto.add(comp);
				contatto.add(contactPanel);
				contatto.addActionListener(action);
				contactContainer.add(contatto);
			}
			contactCount = contactContainer.getComponentCount();
			if (contactCount==0){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("'"+txt+"' non ha prodotto risultati"));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				contactCount = contactContainer.getComponentCount();
			}
		}
		contactContainer.add(Box.createVerticalGlue());

	}

	public static void uppdater() {
		SearchContact.cntAdm.updateForAdmin();
	}

	public static void alertMessage(Integer acc_id) {
		String message ="",nome="",cognome="";
		for (int i=0;i<Database.allAcc.size();i++)
			if (Database.allAcc.get(i).getID() == acc_id){
				nome=Database.allAcc.get(i).getFirstname();
				cognome=Database.allAcc.get(i).getSecondname();
				break;
			}
		message = JOptionPane.showInputDialog(new JFrame(), "Inserisci messaggio per "+nome+ " "+cognome,"Alert",JOptionPane.OK_OPTION);
		if (message!=null)
			if (!message.isEmpty()){
				int res = JOptionPane.showConfirmDialog(new JFrame(), "Sei sicuro di voler inviare l'alert?","ATTENZIONE!",JOptionPane.YES_NO_OPTION);
				if (res==0)
					Engine.sendAlert(message,acc_id);
			}
	}

	public static void stopAdminContactView(){
		contactView.dispose();
	}

	public static void restart(boolean b) {
		if (SearchContact.cntAdm.countObservers()!=0)
			SearchContact.cntAdm.deleteObservers();
		SearchContact.cntAdm.addObserver(obs);
		if (search.getKeyListeners().length!=0)
			search.removeKeyListener(key);
		search.addKeyListener(key);
		search.setName("Admin");
		flag = b;
		uppdater();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if ((Boolean)arg1 == true){
			contact.removeAll();
			startAdminContactView();
			contact.repaint();
			contact.validate();
		}
		//System.out.println("adminupdate");
	}




}
